<template>
  <div class="home">
    <header>
      <span></span>
      <span>
        <span v-show="!show1" @click="flag = true">admin</span>
        <span v-show="show1" @click="exit">adminOut</span>
        <i class="el-icon-shopping-cart-2" @click="toCar"></i>
      </span>
    </header>
    <main>
      <p>
        <span></span>
        <span>
          Sort By:
          <span>Default</span>
          <span v-show="!show" @click="one"
            >Price<i class="el-icon-top"></i
          ></span>
          <span v-show="show" @click="two"
            >Price<i class="el-icon-bottom"></i
          ></span>
        </span>
      </p>
      <div>
        <el-row>
          <el-col :span="4">
            <div class="left">
              <h5>PRICE:</h5>
              <el-menu
                default-active="1"
                class="el-menu-vertical-demo"
                active-text-color="red"
                background-color="ghostwhite"
                @select="onSelect"
              >
                <el-menu-item index="1">
                  <span slot="title">All</span>
                </el-menu-item>
                <el-menu-item index="2">
                  <span slot="title">0.00-100.00</span>
                </el-menu-item>
                <el-menu-item index="3">
                  <span slot="title">100.00-500.00</span>
                </el-menu-item>
                <el-menu-item index="4">
                  <span slot="title">500.00-1000.00</span>
                </el-menu-item>
                <el-menu-item index="5">
                  <span slot="title">1000.00-2000.00</span>
                </el-menu-item>
              </el-menu>
            </div>
          </el-col>
          <el-col :span="19" :offset="1">
            <div class="right">
              <ul @scroll="onScroll">
                <Item :list="list" />
              </ul>
            </div>
          </el-col>
        </el-row>
      </div>
    </main>
    <!-- 登录框 -->
    <el-dialog title="登录" :visible.sync="flag" width="30%">
      <el-form :model="login" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="login.name" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="login.pass"
            type="password"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import Item from "../components/item.vue";
export default {
  data() {
    return {
      list: [],
      listAll: [],
      page: 1,
      show: false,
      show1: false,
      flag: false,
      login: {
        name: "",
        pass: "",
      },
    };
  },
  mounted() {
    this.$axios.get("data.json").then((res) => {
      // console.log(res);
      this.listAll = res.data.result.list;
      this.list = res.data.result.list;
    });
    if (localStorage.getItem("token")) {
      this.show1 = true;
    } else {
      this.show1 = false;
    }
  },
  methods: {
    // 懒加载
    onScroll(e) {
      var clientHeight = e.target.clientHeight;
      var scrollHeight = e.target.scrollHeight;
      var scrollTop = e.target.scrollTop;
      if (scrollHeight - scrollTop - clientHeight <= 1) {
        if (this.page < 3) {
          this.$axios.get(`data${(this.page += 1)}.json`).then((res) => {
            // console.log(res);
            setTimeout(() => {
              res.data.result.list.forEach((item) => {
                this.listAll.push(item);
              });
            }, 50);
          });
        }
      }
    },
    // 筛选
    onSelect(index) {
      // console.log(index);
      if (index == 1) {
        this.list = [];
        this.listAll.forEach((item) => {
          this.list.push(item);
        });
      } else if (index == 2) {
        this.list = [];
        this.listAll.forEach((item) => {
          if (item.salePrice > 0 && item.salePrice < 100) {
            this.list.push(item);
          }
        });
      } else if (index == 3) {
        this.list = [];
        this.listAll.forEach((item) => {
          if (item.salePrice > 100 && item.salePrice < 500) {
            this.list.push(item);
          }
        });
      } else if (index == 4) {
        this.list = [];
        this.listAll.forEach((item) => {
          if (item.salePrice > 500 && item.salePrice < 1000) {
            this.list.push(item);
          }
        });
      } else if (index == 5) {
        this.list = [];
        this.listAll.forEach((item) => {
          if (item.salePrice > 1000 && item.salePrice < 2000) {
            this.list.push(item);
          }
        });
      }
      // this.show = false;
    },
    // 降序
    one() {
      this.show = true;
      this.list.sort((a, b) => {
        return b.salePrice - a.salePrice;
      });
    },
    // 升序
    two() {
      this.show = false;
      this.list.sort((a, b) => {
        return a.salePrice - b.salePrice;
      });
    },
    // 登录
    onSubmit() {
      if (this.login.nmae == "" || this.login.pass == "") {
        this.$message({
          type: "warning",
          message: "账号或密码不能为空",
        });
        return;
      }
      this.show1 = true;
      this.flag = false;
      localStorage.setItem("token", "ajkgfbjabgjabga");
    },
    // 退出
    exit() {
      this.show1 = false;
      localStorage.removeItem("token");
    },
    // 跳转购物车
    toCar() {
      this.$router.push({ path: "/about" });
    },
  },
  components: {
    Item,
  },
};
</script>
<style lang="scss">
* {
  margin: 0;
  padding: 0;
  list-style: none;
}
body {
  background: ghostwhite;
}
header {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  margin: 10px 0;
  box-sizing: border-box;
  padding: 0 30px;
  > span {
    &:nth-child(2) {
      span {
        margin: 0 20px;
        cursor: pointer;
      }
      i {
        font-size: 20px;
        cursor: pointer;
      }
    }
  }
}
main {
  width: 100%;
  > p {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
    box-sizing: border-box;
    padding: 0 10px;
    margin-bottom: 10px;
    > span {
      &:nth-child(2) {
        span {
          margin: 0 5px;
          cursor: pointer;
          &:hover {
            color: red;
          }
        }
      }
    }
  }
  > div {
    width: 100%;
    box-sizing: border-box;
    padding: 0 30px;
    @media screen and(min-width: 1000px) {
      .left {
        height: 500px;
        h5 {
          margin-bottom: 10px;
          font-size: 16px;
        }
        ul {
          border-right: 0 !important;
        }
      }
      .right {
        width: 100%;
        height: 500px;
        background: #eee;
        ul {
          width: 100%;
          height: 100%;
          overflow: auto;
          > div {
            width: 100%;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            flex-wrap: wrap;
            box-sizing: border-box;
            li {
              width: 200px;
              height: 300px;
              background: white;
              margin: 20px;
              box-sizing: border-box;
              border: 1px solid gray;
              padding: 10px;
              &:hover {
                border: 1px solid red;
                box-shadow: 0px 0px 15px 3px gainsboro;
                transform: scale(1.01);
              }
              img {
                width: 100%;
              }
              h4 {
                font-size: 16px;
              }
              p {
                color: red;
                margin: 5px 0;
              }
              div {
                width: 100%;
                height: 30px;
                display: flex;
                justify-content: center;
                align-items: center;
                color: red;
                border: 1px solid red;
                box-sizing: border-box;
                cursor: pointer;
                &:hover {
                  background: red;
                  color: white;
                }
              }
            }
          }
        }
      }
    }
    @media screen and (max-width: 1000px) {
      .left {
        display: none;
      }
      .right {
        width: 100%;
        height: 500px;
        background: #eee;
        ul {
          width: 100%;
          height: 100%;
          overflow: auto;
          > div {
            width: 100%;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            flex-wrap: wrap;
            box-sizing: border-box;
            li {
              width: 100%;
              height: 100px;
              background: white;
              margin: 20px;
              box-sizing: border-box;
              border: 1px solid gray;
              padding: 10px;
              display: flex;
              justify-content: space-between;
              align-items: center;
              &:hover {
                border: 1px solid red;
                box-shadow: 0px 0px 15px 3px gainsboro;
                transform: scale(1.01);
              }
              img {
                height: 100%;
              }
              h4 {
                font-size: 16px;
              }
              p {
                color: red;
                // margin: 5px 0;
              }
              div {
                width: 100px;
                height: 30px;
                display: flex;
                justify-content: center;
                align-items: center;
                color: red;
                border: 1px solid red;
                box-sizing: border-box;
                cursor: pointer;
                &:hover {
                  background: red;
                  color: white;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>